package jsyf.oa.web.doc;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.doc.FileTypeFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Maqiuqiu on 2018/4/24.
 */
@Api(value = "常用文档接口", description = "常用文档接口")
@Controller
@RequestMapping("/docFileType")
public class FileTypeController {

    @Resource
    private FileTypeFacade fileTypeFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String resultId = this.fileTypeFacade.save(jsonData);
        String result = "";
        if ("".equals(resultId)) {
            result = BaseUtils.errorrRespond("服务器错误");
        } else {
            JSONObject obj = new JSONObject();
            obj.put("id", resultId);
            result = BaseUtils.succsessObjRespond(obj);
        }
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = fileTypeFacade.delById(jsonParam.getString("id"));
        if (result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("删除失败！"));
        }
    }

    @RequestMapping(value = "/removeById.json", method = RequestMethod.POST)
    public void removeById( HttpServletResponse response, @RequestBody String jsonData) {
        Boolean records = this.fileTypeFacade.removeById(jsonData);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records.toString()));
    }
}
